﻿#region RDIFramework.NET-generated
//------------------------------------------------------------------------------
//	RDIFramework.NET（.NET快速信息化系统开发、整合框架）是基于.NET的快速信息化系统开发、整合框架，给用户和开发者最佳的.Net框架部署方案。
//	RDIFramework.NET平台包含基础公共类库、强大的权限控制、模块分配、数据字典、自动升级、各商业级控件库、工作流平台、代码生成器、开发辅助
//工具等，应用系统的各个业务功能子系统，在系统体系结构设计的过程中被设计成各个原子功能模块，各个子功能模块按照业务功能组织成单独的程序集文
//件，各子系统开发完成后，由RDIFramework.NET平台进行统一的集成部署。
//
// 官方博客：http://www.cnblogs.com/huyong
//           http://blog.csdn.net/chinahuyong
//    Email：80368704@qq.com
//       QQ：80368704
//------------------------------------------------------------------------------
// <auto-generated>
//	此代码由RDIFramework.NET平台代码生成工具自动生成。
//	运行时版本:4.0.30319.1
//
//	对此文件的更改可能会导致不正确的行为，并且如果
//	重新生成代码，这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Data;

namespace Centaline.BizLogic
{
    using RDIFramework.ServiceAdapter;
    using RDIFramework.Utilities;

    /// <summary>
    /// RepairsManager
    /// 
    /// 
    /// 修改纪录
    /// 
    /// 2014-04-02 版本：1.0  创建主键。
    /// 
    /// 版本：1.0
    /// 
    /// <author>
    /// <name></name>
    /// <date>2014-04-02</date>
    /// </author>
    /// </summary>
    public partial class RepairsManager : DbCommonManager, IDbCommonManager
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public RepairsManager()
        {
            base.CurrentTableName = RepairsTable.TableName;
            base.PrimaryKey = "Id";
        }

        /// <summary>
        /// 构造函数
        /// <param name="tableName">指定表名</param>
        /// </summary>
        public RepairsManager(string tableName)
        {
            base.CurrentTableName = tableName;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbProvider">数据库连接</param>
        public RepairsManager(IDbProvider dbProvider): this()
        {
            DBProvider = dbProvider;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        public RepairsManager(UserInfo userInfo) : this()
        {
            UserInfo = userInfo;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbProvider">数据库连接</param>
        /// <param name="userInfo">用户信息</param>
        public RepairsManager(IDbProvider dbProvider, UserInfo userInfo) : this(dbProvider)
        {
            UserInfo = userInfo;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbProvider">数据库连接</param>
        /// <param name="userInfo">用户信息</param>
        /// <param name="tableName">指定表名</param>
        public RepairsManager(IDbProvider dbProvider, UserInfo userInfo, string tableName) : this(dbProvider, userInfo)
        {
            base.CurrentTableName = tableName;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="repairsEntity">实体</param>
        /// <returns>主键</returns>
        public string Add(RepairsEntity repairsEntity)
        {
            return this.AddEntity(repairsEntity);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="repairsEntity">实体</param>
        /// <param name="identity">自增量方式</param>
        /// <param name="returnId">返回主键</param>
        /// <returns>主键</returns>
        public string Add(RepairsEntity repairsEntity, bool identity, bool returnId)
        {
            this.Identity = identity;
            this.ReturnId = returnId;
            return this.AddEntity(repairsEntity);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="repairsEntity">实体</param>
        public int Update(RepairsEntity repairsEntity)
        {
            return this.UpdateEntity(repairsEntity);
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">主键</param>
        public RepairsEntity GetEntity(string id)
        {
            RepairsEntity repairsEntity = new RepairsEntity(this.GetDT(new KeyValuePair<string, object>(RepairsTable.FieldId, id)));
            return repairsEntity;
        }

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="repairsEntity">实体</param>
        public string AddEntity(RepairsEntity repairsEntity)
        {
            string sequence = string.Empty;
            this.Identity = false; 
            if (repairsEntity.SortCode == null || repairsEntity.SortCode == 0)
            {
                CiSequenceManager sequenceManager = new CiSequenceManager(DBProvider, this.Identity);
                sequence = sequenceManager.GetSequence(this.CurrentTableName);
                repairsEntity.SortCode = int.Parse(sequence);
            }
            if (repairsEntity.Id != null)
            {
                sequence = repairsEntity.Id.ToString();
            }
            SQLBuilder sqlBuilder = new SQLBuilder(DBProvider, this.Identity, this.ReturnId);
            sqlBuilder.BeginInsert(this.CurrentTableName, RepairsTable.FieldId);
            if (!this.Identity) 
            {
                if (string.IsNullOrEmpty(repairsEntity.Id.ToString())) 
                { 
                    sequence = BusinessLogic.NewGuid();
                    repairsEntity.Id = BusinessLogic.ConvertToInt(sequence);
                }
                sqlBuilder.SetValue(RepairsTable.FieldId, repairsEntity.Id);
            }
            else
            {
                if (!this.ReturnId && (DBProvider.CurrentDbType == CurrentDbType.Oracle || DBProvider.CurrentDbType == CurrentDbType.DB2))
                {
                    if (DBProvider.CurrentDbType == CurrentDbType.Oracle)
                    {
                        sqlBuilder.SetFormula(RepairsTable.FieldId, "SEQ_" + this.CurrentTableName.ToUpper() + ".NEXTVAL ");
                    }
                    if (DBProvider.CurrentDbType == CurrentDbType.DB2)
                    {
                        sqlBuilder.SetFormula(RepairsTable.FieldId, "NEXT VALUE FOR SEQ_" + this.CurrentTableName.ToUpper());
                    }
                }
                else
                {
                    if (this.Identity && (DBProvider.CurrentDbType == CurrentDbType.Oracle || DBProvider.CurrentDbType == CurrentDbType.DB2))
                    {
                        if (string.IsNullOrEmpty(repairsEntity.Id.ToString()))
                        {
                            if (string.IsNullOrEmpty(sequence))
                            {
                                CiSequenceManager sequenceManager = new CiSequenceManager(DBProvider, this.Identity);
                                sequence = sequenceManager.GetSequence(this.CurrentTableName);
                            }
                            repairsEntity.Id = BusinessLogic.ConvertToInt(sequence);
                        }
                        sqlBuilder.SetValue(RepairsTable.FieldId, repairsEntity.Id);
                    }
                }
            }
            this.SetEntity(sqlBuilder, repairsEntity);
            if (UserInfo != null) 
            { 
                sqlBuilder.SetValue(RepairsTable.FieldCreateUserId, UserInfo.Id);
                sqlBuilder.SetValue(RepairsTable.FieldCreateBy, UserInfo.RealName);
            } 
            sqlBuilder.SetDBNow(RepairsTable.FieldCreateOn);
            if (UserInfo != null) 
            { 
                sqlBuilder.SetValue(RepairsTable.FieldModifiedBy, UserInfo.RealName);
            } 
            sqlBuilder.SetDBNow(RepairsTable.FieldModifiedOn);
            if (this.Identity && (DBProvider.CurrentDbType == CurrentDbType.SqlServer || DBProvider.CurrentDbType == CurrentDbType.Access))
            {
                sequence = sqlBuilder.EndInsert().ToString();
            }
            else
            {
                sqlBuilder.EndInsert();
            }
            return sequence;
        }

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="repairsEntity">实体</param>
        public int UpdateEntity(RepairsEntity repairsEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(DBProvider);
            sqlBuilder.BeginUpdate(this.CurrentTableName);
            this.SetEntity(sqlBuilder, repairsEntity);
            if (UserInfo != null) 
            { 
                sqlBuilder.SetValue(RepairsTable.FieldModifiedBy, UserInfo.RealName);
            } 
            sqlBuilder.SetDBNow(RepairsTable.FieldModifiedOn);
            sqlBuilder.SetWhere(RepairsTable.FieldId, repairsEntity.Id);
            return sqlBuilder.EndUpdate();
        }

        /// <summary>
        /// 设置实体
        /// </summary>
        /// <param name="repairsEntity">实体</param>
        private void SetEntity(SQLBuilder sqlBuilder, RepairsEntity repairsEntity)
        {
            sqlBuilder.SetValue(RepairsTable.FieldWorkOrderId, repairsEntity.WorkOrderId);
            sqlBuilder.SetValue(RepairsTable.FieldComputerNumber, repairsEntity.ComputerNumber);
            sqlBuilder.SetValue(RepairsTable.FieldArticleId, repairsEntity.ArticleId);
            sqlBuilder.SetValue(RepairsTable.FieldMtStaff, repairsEntity.MtStaff);
            sqlBuilder.SetValue(RepairsTable.FieldFinishMtTime, repairsEntity.FinishMtTime);
            sqlBuilder.SetValue(RepairsTable.FieldApplyMtTime, repairsEntity.ApplyMtTime);
            sqlBuilder.SetValue(RepairsTable.FieldStatusId, repairsEntity.StatusId);
            sqlBuilder.SetValue(RepairsTable.FieldMtAddress, repairsEntity.MtAddress);
            sqlBuilder.SetValue(RepairsTable.FieldApplyMtStaffid, repairsEntity.ApplyMtStaffid);
            sqlBuilder.SetValue(RepairsTable.FieldEmail, repairsEntity.Email);
            sqlBuilder.SetValue(RepairsTable.FieldPhone, repairsEntity.Phone);
            sqlBuilder.SetValue(RepairsTable.FieldWhoArrange, repairsEntity.WhoArrange);
            sqlBuilder.SetValue(RepairsTable.FieldRate, repairsEntity.Rate);
            sqlBuilder.SetValue(RepairsTable.FieldEvaluat, repairsEntity.Evaluat);
            sqlBuilder.SetValue(RepairsTable.FieldReason, repairsEntity.Reason);
            sqlBuilder.SetValue(RepairsTable.FieldDeleteMark, repairsEntity.DeleteMark);
            sqlBuilder.SetValue(RepairsTable.FieldSortCode, repairsEntity.SortCode);
            sqlBuilder.SetValue(RepairsTable.FieldDescription, repairsEntity.Description);
            sqlBuilder.SetValue(RepairsTable.FieldModifyUserId, repairsEntity.ModifyUserId);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>影响行数</returns>
        public int Delete(string id)
        {
            return this.Delete(new KeyValuePair<string, object>(RepairsTable.FieldId, id));
        }
    }
}
